package com.yaha.controler.devices;

import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.NetworkInfo.DetailedState;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.yaha.controler.MyApplication;
import com.yaha.controler.devices.search.MdnsSearchManager;
import com.yaha.controler.utils.MyLog;
import com.yaha.controler.utils.WifiUtil;

public class WifiBroadcastReceiver extends BroadcastReceiver {

	private static final String TAG = WifiBroadcastReceiver.class.getName();

	public WifiBroadcastReceiver() {
		Log.i(TAG, "WifiBroadcastReceiver start");

	}

	@Override
	public void onReceive(final Context context, Intent intent) {
		MyLog.i(TAG, "WifiBroadcastReceiver onReceive-->Begin");
		WifiUtil wifiUtil = new WifiUtil(context);
		WifiInfo wifiInfo = wifiUtil.getmWifiInfo();

		String ssid = WifiUtil.getWifiInfoSSID(wifiInfo);/* wifiInfo.getSSID(); */
		String bssid = wifiInfo.getBSSID();

		Log.i(TAG, "wifiInfo.getIpAddress() = " + wifiUtil.long2ip(wifiInfo.getIpAddress()) + "------>action : "
				+ intent.getAction());
		if (!TextUtils.isEmpty(ssid) && ssid.equals(bssid)) {
			// 下面的代码是为了对88版飞phone的wifiInfo中ssid和bssid相同bug进行的特殊处理
			wifiUtil.startScan();
			List<ScanResult> scanResultList = wifiUtil.getmWifiList();
			if (null != scanResultList) {
				for (ScanResult sr : scanResultList) {
					if (sr.BSSID.equals(bssid)) {
						ssid = sr.SSID;
						break;
					}
				}
			}
		}

		String action = intent.getAction();
		if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {

			/**
			 * 粗略状态（getState）： 详细状态（getDetailedState） disconnected:
			 * idle/disconnected/unavailable/failed connecting:
			 * scanning/connecting/authenticating connected: connected
			 * disconnecting: disconnecting
			 * 
			 */
			MyApplication.wifiState = ((NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO))
					.getDetailedState();

			MyApplication.eventBus.post(DeviceConstants.EVENTBUT_WIFI_STATE_UPDATE);

			MyApplication.curWifiSSID = ssid;
			MyApplication.curWifiBSSID = bssid;

			if (MyApplication.wifiState == DetailedState.CONNECTED) {
				Log.d(TAG, "onReceive ---> CONNECTED");
				
				MdnsSearchManager.newInstance(context).startMDNS();

			} else if (MyApplication.wifiState == DetailedState.DISCONNECTED
					|| MyApplication.wifiState == DetailedState.FAILED) {
				Log.d(TAG, "onReceive ---> DISCONNECTED | FAILED");

				MyApplication.curWifiSSID = null;
				MyApplication.curWifiBSSID = null;

				// MyApplication.wifiState = null;

				// TODO stopHttpServer();

				// 清空设备数据
				// TODO DeviceManager.newInstance().clearMdnsAndDlnaList();

				// TODO MdnsSearchManager.newInstance(context).stopMDNS();
				// DlnaSearchManager.newInstance(context).cleanAway();

				// MyApplication.eventBus.post(DeviceConstants.EVENTBUS_DEVICE_UPDATE);
				// MyApplication.eventBus.post(DeviceConstants.EVENTBUS_WIFI_DISCONNECT);
			} else {
				MyApplication.curWifiSSID = null;
				MyApplication.curWifiBSSID = null;
			}
		}

		if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(action)) {

			Bundle bundle = intent.getExtras();
			int nWifiPwdErrorRet = bundle.getInt(WifiManager.EXTRA_SUPPLICANT_ERROR);
			if (nWifiPwdErrorRet == WifiManager.ERROR_AUTHENTICATING) {

				// TODO MyApplication.passwordError = true;

				// TODO
				// MyApplication.eventBus.post(DeviceConstants.EVENTBUS_WIFI_PWDERROR);
			}
		}

	}
}
